package cn.shun.config;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

@EnableCaching
@Configuration
public class SpringCacheConfig extends CachingConfigurerSupport {
    @Resource(name = "simpleCacheManager")
    private CacheManager simpleCacheManager;

    @Override
    public CacheManager cacheManager() {
        return simpleCacheManager;
    }

    @Bean
    public SimpleCacheManager simpleCacheManager(List<Cache> cacheList) {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(cacheList);
        return cacheManager;
    }

    @Bean
    public ConcurrentMapCacheFactoryBean defaultCacheBean() {
        ConcurrentMapCacheFactoryBean defaultFactoryBean = new
                ConcurrentMapCacheFactoryBean();
        defaultFactoryBean.setName("default");
        return defaultFactoryBean;
    }

    @Bean
    public ConcurrentMapCacheFactoryBean userCacheBean() {
        ConcurrentMapCacheFactoryBean usersFactoryBean = new
                ConcurrentMapCacheFactoryBean();
        usersFactoryBean.setName("users");
        return usersFactoryBean;
    }
}